/*
 * MIT License
 * 
 * Copyright (c) 2017 wen.gu <454727014@qq.com>
 * 
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 * 
 * The above copyright notice and this permission notice shall be included in all
 * copies or substantial portions of the Software.
 * 
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
 * SOFTWARE.
 */

/***************************************************************************
 * Name: tiny_string.h
 *
 * Purpose: string operation define 
 *
 * Developer:
 *   wen.gu , 2016-02-19
 *
 * TODO: 
 *
 ***************************************************************************/

#ifndef __TINY_STRING_H__
#define __TINY_STRING_H__
#include <string.h>

#include "general_type.h"

#ifdef __cplusplus
extern "C"{
#endif

/***************************************************************************
 *
 * macro declaration
 *
 ***************************************************************************/
#if defined(_MSC_VER) //visual studio compat implement
#define snprintf _snprintf_c
#define strncasecmp _strnicmp
#define strcasecmp _stricmp
#define atoll _atoi64
#define strdup _strdup
#endif
/***************************************************************************
 *
 * data struct declaration
 *
 ***************************************************************************/


/***************************************************************************
 *
 * API declaration
 *
 ***************************************************************************/
 /**
 *@brief  Copy the string src to dst, but no more than size - 1 bytes, and
 *        null-terminate dst.
 *
 * @param dst [io]  destination buffer
 * @param src [in]  source string
 * @param size [in] size of destination buffer
 *
 *@return the length of src.
 *@see 
 *@note function is the same as BSD strlcpy().
 *@warning since the return value is the length of src, src absolutely
 * _must_ be a properly 0-terminated string, otherwise this will read beyond
 * the end of the buffer and possibly crash.
 */

G_API GU32 tfStrlcpy(char *dst, const char *src, GU32 size);

/**
*@brief  get char 16 string length
*
* @param str [in]  char 16 string
*
*@return the length of str.
*@see
*/
G_API GU32 tfStrLen16(const GS16 *str);

/**
*@brief  get char 16 string length with max count limit
*
* @param str      [in]  char 16 string
*        maxcount [in]  max count limit
*
*@return the length of str.
*@see
*/
G_API GU32 tfStrnLen16(const GS16 *str, GU32 maxcount);

/**
*@brief  copy char 16 string 
*
* @param dst [io] destination char 16 string
*        src [in] source char 16 string 
*
*@return source char 16 string length.
*@see
*/
G_API GU32 tfStrCpy16(GS16 *dst, const GS16 *src);

/**
*@brief  copy a char 8 string to char 16 string
*
* @param dst [io] destination char 16 string
*        src [in] source char 8 string
*
*@return source char 8 string length.
*@see
*/
G_API GU32 tfStrCpy16_8(GS16 *dst, const GS08 *src);

/**
*@brief  copy char 16 string with max count limit
*
* @param dst [io] destination char 16 string
*        src [in] source char 16 string
*        maxcount [in] max count limit
*
*@return source char 16 string length or maxcount.
*@see
*/
G_API GU32 tfStrnCpy16(GS16 *dst, const GS16 *src, GU32 maxcount);

/**
*@brief  copy 'count' number of char 16  
*
* @param dst [io] destination char 16 string
*        src [in] source char 16 string
*        count [in] the number of char 16
*
*@return None.
*@see
*/
G_API void tfStrMemCpy16(GS16 *dst, const GS16 *src, GU32 count);

/**
*@brief  memory set with 'value'
*
* @param dst   [io] destination char 16 string
*        value [in] the value of a char 16
*        count [in] the number of char 16
*
*@return None.
*@see
*/
G_API void tfStrMemSet16(GS16 *dst, GS16 value, GU32 count);

/**
*@brief  compare two char 16 string
*
* @param str1 [in] a char 16 string
*        str2 [in] a char 16 string
*
*@return str1 < str2 : return negative number(-1) 
*        str1 == str2: return 0.
*        str1 > str2 : return positive number(1)  
*@see
*/
G_API GS32  tfStrCmp16(const GS16 *str1, const GS16 *str2);

/**
*@brief  compare a char 16 string and a char 8 string
*
* @param str1 [in] a char 16 string
*        str2 [in] a char 8 string
*
*@return str1 < str2 : return negative number(-1)
*        str1 == str2: return 0.
*        str1 > str2 : return positive number(1)
*@see
*/
G_API GS32  tfStrCmp16_8(const GS16 *str1, const GS08 *str2);

/**
*@brief  convert a char 16 string to uppercase
*
* @param str [io] a char 16 string
*        len [in] char 16 string length
*
*@return None.
*@see
*/
G_API void tfStrUppercase16(GS16 *str, GS32 len);

/**
*@brief  convert a char 16 string to lowercase
*
* @param str [io] a char 16 string
*        len [in] char 16 string length
*
*@return None.
*@see
*/
G_API void tfStrLowercase16(GS16 *str, GS32 len);

//////////////////////////////////////////////////////////////////////////
/**
*@brief get char 8 string length
*
* @param str [in] char 8 string
* @return
*
*@return the length of str.
*@see
*/
G_API GU32 tfStrLen(const GS08 *str);

/**
*@brief  get char 8 string length with max count limit
*
* @param str      [in]  char 8 string
*        maxcount [in]  max count limit
*
*@return the length of str.
*@see
*/
G_API GU32 tfStrnLen(const GS08 *str, GU32 maxcount);

/**
*@brief  copy char 8 string
*
* @param dst [io] destination char 8 string
*        src [in] source char 8 string
*
*@return source char 8 string length.
*@see
*/
G_API GU32 tfStrCpy(GS08 *dst, const GS08 *src);

/**
*@brief  copy char 8 string with max count limit
*
* @param dst [io] destination char 8 string
*        src [in] source char 8 string
*        maxcount [in] max count limit
*
*@return source char 8 string length or maxcount.
*@see
*/
G_API GU32 tfStrnCpy(GS08 *dst, const GS08 *src, GU32 maxcount);

/**
*@brief  copy 'count' number of char 8
*
* @param dst   [io] destination char 8 string
*        src   [in] source char 8 string
*        count [in] the number 8 char 8
*
*@return None.
*@see
*/
G_API void tfStrMemCpy(GS08 *dst, const GS08 *src, GU32 count);

/**
*@brief  memory set with 'value'
*
* @param dst   [io] destination char 8 string
*        value [in] the value of a char 8
*        count [in] the number of char 8
*
*@return None.
*@see
*/
G_API void tfStrMemSet(GS08 *dst, GS08 value, GU32 count);

/**
*@brief  compare two char 8 string
*
* @param str1 [in] a char 8 string
*        str2 [in] a char 8 string
*
*@return str1 < str2 : return negative number(-1)
*        str1 == str2: return 0.
*        str1 > str2 : return positive number(1)
*@see
*/
G_API GS32  tfStrCmp(const GS08 *str1, const GS08 *str2);

/**
*@brief  compare a char 8 string and a char 16 string
*
* @param str1 [in] a char 8 string
*        str2 [in] a char 16 string
*
*@return str1 < str2 : return negative number(-1)
*        str1 == str2: return 0.
*        str1 > str2 : return positive number(1)
*@see
*/
G_API GS32 tfStrCmp8_16(const GS08 *str1, const GS16 *str2);



#ifdef __cplusplus
}
#endif
#endif //end of __TINY_STRING_H__
